package com.gitee.secretopen.sso.controller;

import cn.chenc.framework.core.exception.CustomException;
import cn.chenc.framework.kaptcha.Captcha;
import cn.chenc.framework.kaptcha.GifCaptcha;
import com.gitee.secretopen.sso.exception.CustomCode;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;

/**
 * @Author Xjt
 * @Date 2021/2/28 21:02
 * @Version 1.0
 */
@RestController
public class ValidCodeController {

    @GetMapping("/captchaImage")
    @ResponseBody
    public void validCode(HttpServletResponse response) throws CustomException {
        try {
            response.setHeader("Pragma", "No-cache");
            response.setHeader("Cache-Control", "no-cache");
            response.setDateHeader("Expires", 0);
            response.setContentType("image/gif");
            /**
             * gif格式动画验证码
             * 宽，高，位数。
             */
            Captcha captcha = new GifCaptcha(119,33,4);
            //输出
            captcha.out(response.getOutputStream());
        } catch (Exception e) {
            e.printStackTrace();
            new CustomCode(false,500,"获取验证码异常："+e.getMessage());
        }
    }
}
